<?php

namespace App\Admin\Controllers;

use App\Models\Cashier;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Hash;

class CashierController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '收银员';

    protected $status = [
        'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
        'off' => ['value' => 2, 'text' => '禁用', 'color' => 'danger'],
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cashier());

        $grid->numbers('序号');
        $grid->rows(function ($row, $number) {
            $row->column('numbers', $number+1);
        });

        $grid->column('name', __('Name'));
        $grid->column('number', __('Number'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('username', __('Username'));
        $grid->column('status', __('Status'))
            ->switch($this->status);
        $grid->column('turnover', __('Turnover'))->display(function($data){
            return '￥'.$data;
        })->sortable();
        $grid->column('grand_total', __('Grand total'))->sortable();
        $grid->column('cash_total', __('Cash total'))->display(function($data){
            return '￥'.$data;
        })->sortable();
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', '姓名');
            $filter->like('mobile', '手机');

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Cashier::findOrFail($id));

        $show->field('name', __('Name'));
        $show->field('number', __('工号'));
        $show->field('mobile', __('Mobile'));
        $show->field('username', __('Username'));
//        $show->field('password', __('Password'));
        $show->field('status', __('Status'))->using([
            '1' => '正常',
            '2' => '修整'
        ]);
        $show->field('turnover', __('Turnover'));
        $show->field('grand_total', __('Grand total'));
        $show->field('cash_total', __('Cash total'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Cashier());




        $form->text('name', __('Name'))->required();
        
        if ($form->isCreating()){
            $form->text('number', '工号')->rules(function ($form) {
                // 如果不是编辑状态，则添加字段唯一验证
                if (!$id = $form->model()->id) {
                    return 'required|unique:cashiers,number';
                }
            });

        }
        

        if ($form->isEditing()){
            $form->display('number',__('工号'))->required();
        }
		$form->hidden('store_id',__('Store_id'));
        $form->mobile('mobile', __('Mobile'))->rules("required|regex:/^1[34578]{1}\d{9}$/");
        $form->text('username', __('Username'))->rules("required");
        $form->password('password', __('Password'))->rules("required");
        $form->switch('status', __('Status'))
            ->options($this->status)->default(1);

        $form->currency('turnover', __('Turnover'))
            ->symbol('￥');

        $form->number('grand_total', __('Grand total'))
            ->default(0);

        $form->currency('cash_total', __('Cash total'))
            ->symbol('￥');
        
        $form->saving(function (Form $form) {
            $form->store_id = Admin::user()->store_id;
            $form->password = Hash::make($form->password);
        });

        return $form;
    }
}
